package words

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/errors/gerror"
	"star/internal/dao"
	"star/internal/model"
	"star/internal/model/do"
	"star/internal/model/entity"
)

//查看单词是否存在

func checkWord(ctx context.Context, id uint, in *model.WordInput) error {

	db := dao.Words.Ctx(ctx).Where("uid", in.Uid).Where("word", in.Word)
	if id > 0 {
		db = db.WhereNot("id", id)
	}
	word, err := db.Count()
	if err != nil {
		return err
	}
	if word > 0 {
		return gerror.New("单词已存在")
	}
	return nil
}

// 新增单词

func CreateWord(ctx context.Context, in *model.WordInput) error {
	if err := checkWord(ctx, 0, in); err != nil {
		return err
	}
	_, err := dao.Words.Ctx(ctx).Data(do.Words{
		Uid:                in.Uid,
		Word:               in.Word,
		Definition:         in.Definition,
		ExampleSentence:    in.ExampleSentence,
		ChineseTranslation: in.ChineseTranslation,
		Pronunciation:      in.Pronunciation,
		ProficiencyLevel:   in.ProficiencyLevel,
	}).Insert()
	if err != nil {
		return err
	}
	return nil
}

//更新单词

func UpdateWord(ctx context.Context, id uint, in *model.WordInput) error {
	if err := checkWord(ctx, id, in); err != nil {
		return err
	}

	db := dao.Words.Ctx(ctx).Data(do.Words{
		Word:               in.Word,
		Definition:         in.Definition,
		ExampleSentence:    in.ExampleSentence,
		ChineseTranslation: in.ChineseTranslation,
		Pronunciation:      in.Pronunciation,
		ProficiencyLevel:   in.ProficiencyLevel,
	}).Where("id", id)
	if in.Uid > 0 {
		db = db.Where("uid", in.Uid)
	}

	_, err := db.Update()
	if err != nil {
		return err
	}
	return nil
}

//获取单词列表

func GetWordList(ctx context.Context, in *model.WordQuery) (list []entity.Words, total uint, err error) {
	if in == nil {
		in = &model.WordQuery{}
	}
	//	初始值
	if in.Page == 0 {
		in.Page = 1
	}
	if in.Size == 0 {
		in.Size = 10
	}
	db := dao.Words.Ctx(ctx)
	if in.Uid > 0 {
		db = db.Where("uid", in.Uid)
	}
	//	查询
	if len(in.Word) > 0 {
		db = db.WhereLike("word", fmt.Sprintf("%%%s%%", in.Word))
	}
	db = db.Order("created_at desc,id desc").Page(in.Page, in.Size)
	data, totalInt, err := db.AllAndCount(true)

	if err != nil {
		return
	}
	list = []entity.Words{}
	err = data.Structs(&list)
	if err != nil {
		return
	}
	total = uint(totalInt)
	return
}

// 获取单词详情

func GetWordDetail(ctx context.Context, uid, id uint) (derail entity.Words, err error) {
	db := dao.Words.Ctx(ctx).Where("id", id)

	if uid > 0 {
		db = db.Where("uid", uid)
	}
	err = db.Scan(&derail)
	if err != nil {
		return derail, gerror.New("单词ID不存在")
	}
	return derail, err
}

// 删除单词

func DelegatorWord(ctx context.Context, uid, id uint) error {
	db := dao.Words.Ctx(ctx).Where("id", id)
	if uid > 0 {
		db.Where("uid", uid)
	}
	_, err := db.Delete()
	return err
}

//随机获取单词

func GetRandWords(ctx context.Context, uid, limit uint) (list []entity.Words, err error) {
	if limit == 0 {
		limit = 50
	}
	list = []entity.Words{}
	db := dao.Words.Ctx(ctx)
	if uid > 0 {
		db = db.Where("uid", uid)
	}
	err = db.OrderRandom().Limit(int(limit)).Scan(&list)
	return
}

//设置单词掌握程度

func SetLevel(ctx context.Context, uid, id uint, level int) error {
	if level < 0 || level > 5 {
		return gerror.New("掌握程度不正确")
	}
	db := dao.Words.Ctx(ctx)
	if uid > 0 {
		db = db.Where("uid", uid)
	}
	_, err := db.Data("proficiency_level", level).Where("id", id).Update()

	return err
}
